package com.google.code.server4j.parser;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Element;
import com.google.code.server4j.thread.ThreadPool;

public class ThreadPoolNode {

	private Map<String, ThreadPool> map = new HashMap<String, ThreadPool>();

	public void assembleThreadPool(Element element) {

		for (Iterator<?> ieson = element.elementIterator(); ieson.hasNext();) {
			Element elementSon = (Element) ieson.next();

			String name = elementSon.getName();
			if ("create".equals(name)) {
				parseCreateConfig(elementSon);
			}

		}
	}

	private void parseCreateConfig(Element element) {
		String id = null;
		ThreadPool pool = null;
		for (Iterator<?> ia = element.attributeIterator(); ia.hasNext();) {
			Attribute attribute = (Attribute) ia.next();

			String name = attribute.getName();
			Object value = attribute.getData();
			if ("id".equals(name)) {
				id = (String) value;
			} else if ("class".equals(name)) {
				String className = (String) value;
				try {
					@SuppressWarnings("unchecked")
					Class<ThreadPool> clazz = (Class<ThreadPool>) Class.forName(className);
					pool = clazz.newInstance();
					map.put(id, pool);
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}//end for
		
		for (Iterator<?> ieson = element.elementIterator(); ieson.hasNext();) {
			Element elementSon = (Element) ieson.next();
			String name = elementSon.getName();
			if ("set".equals(name)) {
				parseSet(elementSon,pool);
			}

		}
	}
	
	/**
	 * 解析参数的Class类型
	 * @param value
	 * @return
	 */
	public Class<?> parseType(String value) {
		try {
			Integer.parseInt(value);
			return Integer.TYPE;
		}catch(Exception e) {
			//ignored
		}
		try {
			Long.parseLong(value);
			return Long.TYPE;
		}catch(Exception e) {
			//ignored
		}
		try {
			Float.parseFloat(value);
			return Float.TYPE;
		}catch(Exception e) {
			//ignored
		}	
		try {
			Double.parseDouble(value);
			return Double.TYPE;
		}catch(Exception e) {
			//ignored
		}
		try {
			Boolean.parseBoolean(value);
			return Boolean.TYPE;
		}catch(Exception e) {
			//ignored
		}			
		return java.lang.String.class;
	}
	
	/**
	 * 将参数类型解析为具体类型并转型为Object返回
	 * @param value
	 * @return
	 */
	public Object parseParmeter(String value) {
		try {
			return Integer.parseInt(value);
		}catch(Exception e) {
			//ignored
		}
		try {
			return Long.parseLong(value);
		}catch(Exception e) {
			//ignored
		}
		try {
			return Float.parseFloat(value);
		}catch(Exception e) {
			//ignored
		}	
		try {
			return Double.parseDouble(value);
		}catch(Exception e) {
			//ignored
		}
		try {
			return Boolean.parseBoolean(value);
		}catch(Exception e) {
			//ignored
		}			
		return value;
	}
	
	
	public void parseSet(Element element,ThreadPool pool) {
		for (Iterator<?> ia = element.attributeIterator(); ia.hasNext();) {
			Attribute attribute = (Attribute) ia.next();

			String methodName = (String)attribute.getData();
			String value = element.getText();
//			System.out.println("set ---"+ methodName+" value="+value);
			String name = "set" + methodName.substring(0,1).toUpperCase() + methodName.substring(1);
			Method method = null;
			Class<?> clazz = pool.getClass();
			
			Class<?>[] valueClass = {parseType(value)};
			Object[] valueObject = {parseParmeter(value)};
			
			try {
				method = clazz.getMethod(name,valueClass);
				System.out.println(method);
				System.out.println(valueClass[0]);
				
				method.invoke(pool, valueObject);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
			
		}
	}

	public Map<String, ThreadPool> getThreadPoolComponent() {
		return new HashMap<String, ThreadPool>(map);
	}
	
	
}
